<?php
class AntiCaptcha extends Module {
    protected $name = 'AntiCaptcha';
    protected $administrable = false;
    protected $version = '2011-07-01';
    protected $details = array('dependencies' => array(),
            'observes' => array());
    
    public function initDisplayBlank($param = null) {
        $this->js('/ajax/content/anti-captcha/', false);
        $_SESSION['anticaptchacode'] = md5(rand(1000000, 9999999));
    }
    
    public function displayBlank($param = null) {
        @header('Content-Type: text/javascript; charset=UTF-8');
        $script = file_get_contents($this->getDir('/anticaptcha.js'));
        if (!isset($_SESSION['anticaptchacode'])) {
            $_SESSION['anticaptchacode'] = md5(rand(1000000, 9999999));
        }
        $script = str_replace('{$CODE}', $_SESSION['anticaptchacode'],
                $script);
        echo $script;
    }
    
    public function checkCode($code = null) {
        if ($code === null) {
            $code = $_REQUEST['anticaptchacode'];
        }
        return $_SESSION['anticaptchacode'] == $code;
    }
}
?>
